package br.usp.icmc.biomac.client.view.impl;

import java.util.Comparator;
import java.util.List;

import br.usp.icmc.biomac.client.view.ROIImageCellTable;
import br.usp.icmc.biomac.shared.ROI;
import br.usp.icmc.biomac.shared.Slider;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SelectionModel;
import com.google.gwt.view.client.SingleSelectionModel;

public class ROIImageCellTableImpl extends Composite implements
		ROIImageCellTable {

	private static ROIImageCellTableUiBinder uiBinder = GWT
			.create(ROIImageCellTableUiBinder.class);
	@UiField(provided = true)
	CellTable<ROI> cellTable = new CellTable<ROI>();
	@UiField
	SimplePager pager1;
	@UiField
	SimplePager pager2;
	@UiField
	  Slider slider;


	Presenter presenter;

	// Create a data provider.
	ListDataProvider<ROI> dataProvider;// = new ListDataProvider<ROI>();

	interface ROIImageCellTableUiBinder extends
			UiBinder<Widget, ROIImageCellTableImpl> {
	}

	public ROIImageCellTableImpl(ListDataProvider<ROI> dataProvider) {
		this.dataProvider = dataProvider;
		initWidget(uiBinder.createAndBindUi(this));
		// cellTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
		cellTable.setWidth("100%", true);


		// Connect the table to the data provider.
		this.dataProvider.addDataDisplay(cellTable);

		// Create a Pager to control the table.
		pager1.setDisplay(cellTable);
		pager2.setDisplay(cellTable);

		// Attach a column sort handler to the ListDataProvider to sort the
		// list.
		ListHandler<ROI> sortHandler = new ListHandler<ROI>(
				dataProvider.getList());
		cellTable.addColumnSortHandler(sortHandler);

		// Add a selection model so we can select cells.

		final SingleSelectionModel<ROI> selectionModel = new SingleSelectionModel<ROI>();
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					@Override
					public void onSelectionChange(SelectionChangeEvent event) {
						ROI selected = selectionModel.getSelectedObject();
						if (selected != null) {
							// setThubmanais(selected.getSeries());
						}
					}
				});

		cellTable.setSelectionModel(selectionModel);

		// Initialize the cell table
		initTableColumns(selectionModel, sortHandler);
	}
	

	@UiHandler("slider")
	void onChangeSlider(ValueChangeEvent<Integer> e) {
		// get the new value
		console("slider change " + slider.getValue());
	}
	
	
	private static native void console(String message) /*-{
    	console.log(message);
	}-*/; 


	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}

	@Override
	public void setDataResult(List<ROI> data) {

		// Add the data to the data provider, which automatically pushes it to
		// the widget.
		List<ROI> list = dataProvider.getList();
		list.clear();
		for (ROI roi : data) {
			list.add(roi);
		}
		slider.update(1,list.size(),1, 1);
	}

	public void initTableColumns(final SelectionModel<ROI> selectionModel,
			ListHandler<ROI> sortHandler) {

		// Create studyUID column.
		Column<ROI, String> labelROI = new Column<ROI, String>(
				new EditTextCell()) {
			@Override
			public String getValue(ROI object) {
				return object.getLabel();
			}
		};
		// Make the studyUID column sortable.
		labelROI.setSortable(true);
		sortHandler.setComparator(labelROI, new Comparator<ROI>() {
			@Override
			public int compare(ROI arg0, ROI arg1) {
				return arg0.getLabel().compareTo(arg1.getLabel());
			}
		});
		// Create a value updater that will be called when the value in a cell
		// changes.
		labelROI.setFieldUpdater(new FieldUpdater<ROI, String>() {
			@Override
			public void update(int index, ROI object, String value) {
				// Window.alert("You changed the Label of " + object.getLabel()
				// + " to " + value);
				// Push the changes into the Contact. At this point, you could
				// send an
				// asynchronous request to the server to update the database (on
				// the server side).
				presenter.edtROI(presenter.getIndexImage(), index, value);
				// old way
				// object.setLabel(value);
				// Redraw the table with the new data.
				cellTable.redraw();
			}
		});

		// add studyUID in cellTable
		cellTable.addColumn(labelROI, "Label");
		cellTable.setColumnWidth(labelROI, 40, Unit.PX);
		
		// Create delRoi column.
		Column<ROI, String> delRoi = new Column<ROI, String>(new ButtonCell()) {
			@Override
			public String getValue(ROI object) {
				// TODO Auto-generated method stub
				return "Delete";
			}
		};
		delRoi.setFieldUpdater(new FieldUpdater<ROI, String>() {
			@Override
			public void update(int index, ROI object, String value) {
				Window.alert(object.getLabel());
				presenter.rmvROI(presenter.getIndexImage(), index);
			}
		});
		cellTable.addColumn(delRoi, "Options");
		cellTable.setColumnWidth(delRoi, 30, Unit.PX);

	}

	@Override
	public void redraw() {
		cellTable.redraw();
	}
	
	
}
